package com.ubis.ibms.plugin.composites.performance;

import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.window.Window;
import org.eclipse.nebula.widgets.calendarcombo.CalendarCombo;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.ubis.ibms.plugin.common.viewer.TableContentProvider;
import com.ubis.ibms.plugin.common.viewer.TableLabelProvider;
import com.ubis.ibms.plugin.dao.RechargeManager;
import com.ubis.ibms.plugin.dialogs.CompanyChooseDialog;
import com.ubis.ibms.plugin.dialogs.RechargeAddDialog;
import com.ubis.ibms.plugin.model.Company;
import com.ubis.ibms.plugin.model.RechargeLog;
import com.ubis.ibms.plugin.model.SeqGenereter;

/**
 * 充值查询
 * @author diamond-wang
 *
 */
public class PerformanceAssesComposite extends Composite {
	
	/*充值*/
	private Button addButton;
	/*公司名称*/
	private Text nameText;
	// 选择公司
	private Button chooseCompanyButton;
	/*起始时间*/
	private CalendarCombo dateFromCombo;
	/*截止时间*/
	private CalendarCombo dateToCombo;
	/*查询按钮*/
	private Button queryButton;
	/*清空*/
	private Button clearButton;
	
	private TableViewer tableViewer;
	private Table table;
	
	private Company company;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public PerformanceAssesComposite(Composite parent, int style) {
		super(parent, style);
		
		GridLayout layout = new GridLayout(1, false);
		layout.marginWidth = 10;
		layout.marginHeight = 10;
		setLayout(layout);
		
		Label title = new Label(this, SWT.NONE);
		title.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.BOLD));
		title.setText("充值记录");
		
		Composite queryComposite = new Composite(this, SWT.BORDER);
		queryComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		queryComposite.setLayout(new GridLayout(6, false));
		
		// 公司名称
		Label nameLabel = new Label(queryComposite, SWT.RIGHT);
		nameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		nameLabel.setText("公司名称：");
		nameText = new Text(queryComposite, SWT.BORDER);
		nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2,
				1));
		nameText.setEditable(false);
		
		// 选择公司
		chooseCompanyButton = new Button(queryComposite, SWT.NONE);
		chooseCompanyButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER,
				false, false, 1, 1));
		chooseCompanyButton.setText("选择公司");
		//点击弹出选择公司窗口
		chooseCompanyButton.addMouseListener(new MouseAdapter(){
			public void mouseUp(MouseEvent e) {
				CompanyChooseDialog companyDialog = new CompanyChooseDialog(PerformanceAssesComposite.this.getShell());
				companyDialog.create();
				if (companyDialog.open() == Window.OK) {
					company = companyDialog.getCompany();
					if(company!=null){
						nameText.setText(company.getName());
					}
				}
			}
		});
		
		addButton = new Button(queryComposite, SWT.NONE);
		addButton.setText("充值");
		addButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false,
				1, 1));
		addButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				// 点击充值时
				if(company!=null){//未先选业主时弹出警告
					RechargeAddDialog addDialog = new RechargeAddDialog(PerformanceAssesComposite.this.getShell(), company);
					addDialog.open();
				}
				else{
					MessageDialog.openWarning(PerformanceAssesComposite.this.getShell(), "警告", "请先选择业主！");
				}
			}
		});
		new Label(queryComposite, SWT.NONE);
		
		// 起始日期
		Label dateFromLabel = new Label(queryComposite, SWT.RIGHT);
		dateFromLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		dateFromLabel.setText("起始时间：");
		dateFromCombo = new CalendarCombo(queryComposite, SWT.BORDER);
		dateFromCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		
		// 截止日期
		Label dateToLabel = new Label(queryComposite, SWT.RIGHT);
		dateToLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		dateToLabel.setText("截止时间：");
		dateToCombo = new CalendarCombo(queryComposite, SWT.BORDER);
		dateToCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		
		//查询按钮
		queryButton = new Button(queryComposite, SWT.NONE);
		queryButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				if(company!=null){//未选择业主时弹出警告
					String begin = dateFromCombo.getDateAsString();
					String end = dateToCombo.getDateAsString();
					SeqGenereter.clearseq();
					List<RechargeLog> recharges = RechargeManager.getRechargeLogs(company.getId(),begin,end);
					tableViewer.setInput(recharges);
				}
				else{
					MessageDialog.openWarning(PerformanceAssesComposite.this.getShell(), "警告", "请先选择业主！");
				}
			}
		});
		queryButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));
		queryButton.setText("查询");
		
		//清空按钮
		clearButton = new Button(queryComposite, SWT.NONE);
		clearButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				clearInput();
			}
		});
		clearButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));
		clearButton.setText("清空");
		
		tableViewer = new TableViewer(this, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		TableViewerColumn seqViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn seqTableColumn = seqViewerColumn.getColumn();
		seqTableColumn.setResizable(false);
		seqTableColumn.setAlignment(SWT.CENTER);
		seqTableColumn.setWidth(40);
		seqTableColumn.setText("序号");
		
		TableViewerColumn nameViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn nameTableColumn = nameViewerColumn.getColumn();
		nameTableColumn.setAlignment(SWT.CENTER);
		nameTableColumn.setWidth(200);
		nameTableColumn.setText("公司名称");
		
		TableViewerColumn timeViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn timeTableColumn = timeViewerColumn.getColumn();
		timeTableColumn.setResizable(false);
		timeTableColumn.setAlignment(SWT.CENTER);
		timeTableColumn.setWidth(150);
		timeTableColumn.setText("充值时间");
		
		TableViewerColumn moneyViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn moneyTableColumn = moneyViewerColumn.getColumn();
		moneyTableColumn.setResizable(false);
		moneyTableColumn.setAlignment(SWT.CENTER);
		moneyTableColumn.setWidth(100);
		moneyTableColumn.setText("充值金额(元)");
		
		TableViewerColumn balanceViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn balanceTableColumn = balanceViewerColumn.getColumn();
		balanceTableColumn.setResizable(false);
		balanceTableColumn.setAlignment(SWT.CENTER);
		balanceTableColumn.setWidth(100);
		balanceTableColumn.setText("充值后余额(元)");
		
		TableViewerColumn userViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn userTableColumn = userViewerColumn.getColumn();
		userTableColumn.setResizable(false);
		userTableColumn.setAlignment(SWT.CENTER);
		userTableColumn.setWidth(100);
		userTableColumn.setText("操作人");
		
		tableViewer.setContentProvider(new TableContentProvider());
		tableViewer.setLabelProvider(new TableLabelProvider());
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	
	/**
	 * 清空输入
	 */
	public void clearInput(){
		nameText.setText("");
		dateFromCombo.setText("");
		dateToCombo.setText("");
		tableViewer.setInput(null);
		company = null;
	}
}
